package com.dsx.test.async.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author : tianwenqing
 * @version : 1.0
 * @date : 2019-09-26 19:46
 * @description :
 **/

@Service
@Slf4j
public class AsyncTestService {


    @Autowired(required = false)
    @Qualifier(value = "taskExecutor")
    private TaskExecutor taskExecutor;

    public void testsync() {
        log.info("==========2");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("==========3");
    }

    @Async
    public void testAsync() {
        log.info("==========2");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("==========3");
    }


    @Async
    public void testAsync1() {
        ThreadPoolTaskExecutor tpe = ((ThreadPoolTaskExecutor) taskExecutor);
//        log.info("----------线程池中的目前存在的线程数量：{}", tpe.getThreadPoolExecutor().getPoolSize());
//        log.info("----------线程池中队列大小：{}",tpe.getThreadPoolExecutor().getQueue().size());
//        log.info("----------线程池中正在执行任务的线程数量：{}",tpe.getThreadPoolExecutor().getActiveCount());
//        log.info("----------线程池中需要执行的任务数量：{}",tpe.getThreadPoolExecutor().getTaskCount());
//        log.info("----------线程池中完成的任务数量：{}",tpe.getThreadPoolExecutor().getCompletedTaskCount());
        log.info("=========={}", Thread.currentThread().getName());
    }

}
